home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Utilities / Programming / EnterAct 3.5 / Drag_on Modules / hAWK programs / MFS_SuperReplace.T < prev    next >
Encoding:
Text File  |  1993-02-08  |  3.1 KB  |  111 lines  |  [TEXT/KEEN]

  1. #MFS_SuperReplace.T
  2. #
  3. ############ THIS IS NOT AN EXECUTABLE PROGRAM ##############
  4. #
  5. #the program to run for "superReplacing" is called $MFS_SuperReplace.
  6. #This is the "Template" from which the Actual program
  7. #(MFS_SuperReplace.A) is produced by $MFS_SuperReplace. In the line
  8. #    while (match(multi, find) > 0)
  9. #"find" is replaced by the actual find string, and in the line
  10. #    sub(find, replace, multi);
  11. #both the find and replace strings are replaced. This produces a program
  12. #with static rather than dynamic regular expression to search for,
  13. #and a constant replace string, and it runs faster as a result.
  14.  
  15. BEGIN {
  16.         if (ignorecase+0 != 0)
  17.             IGNORECASE = 1;
  18.         ORS = "";#so that "print" does not spit out "\n's"
  19.         progressFile = STDPATH "$tempProgress"
  20.     }
  21.  
  22. FNR == 1 {    if (n > 0)
  23.                 {
  24.                 for (i = 1; i <= n; ++i)
  25.                     print out[i] > outfile;
  26.                 if (multi != "")
  27.                     print multi > outfile;
  28.                 print "\n" > outfile;
  29.                 close(outfile);
  30.                 for (i in out)
  31.                     delete out[i];
  32.                 n = 0;
  33.                 }
  34.             outfile = FILENAME;
  35.             if (document != 0)
  36.                 {
  37.                 z = split(FILENAME, names, ":");
  38.                 if (!progress("\nSuperReplacing: " names[z] "\nFiles left: " num_files))
  39.                     { # concurrent mode, print progress to file
  40.                     print "SuperReplacing:", names[z], "\nFiles left:", num_files > progressFile
  41.                     close(progressFile)
  42.                     }
  43.                 --num_files
  44.                 }
  45.         }
  46.  
  47.     {multi = $0;#the first line is already there
  48.     outLine = multiLines = 1;
  49.     #For document purposes, outLine tracks the true output line number of
  50.     #the first line in multi, ie the line number after replacement
  51.     while (getline x > 0)#should exit if < 0
  52.         {
  53.         multi = multi "\n" x;
  54.         ++multiLines;
  55.         while (match(multi, find) > 0)##MARK find
  56.             {
  57.             if (document != 0)#print filename, new line number, text before changes
  58.                 {
  59.                 ++numDid;
  60.                 if (RSTART > 1)
  61.                     {
  62.                     prematch = substr(multi, 1, RSTART - 1);
  63.                     t1 = gsub(/\n/, "\n", prematch);
  64.                     temp = t1 + outLine;
  65.                     }
  66.                 else
  67.                     temp = outLine;
  68.                 print names[z], temp, "  was:" "\n";
  69.                 print substr(multi, RSTART, RLENGTH) "\n";
  70.                 }
  71.             lenBefore = length(multi)
  72.             sub(find, replace, multi);##MARK repToo
  73.             repLen = RLENGTH + length(multi) - lenBefore
  74.             out[++n] = substr(multi, 1, RSTART + repLen - 1);
  75.             if (document != 0)
  76.                 {
  77.                 t1 = gsub(/\n/, "\n", out[n]);
  78.                 outLine += t1;
  79.                 }
  80.             multi = substr(multi, RSTART + repLen);
  81.             #that may have messed up the number of lines
  82.             multiLines = gsub(/\n/, "\n", multi) + 1;
  83.             }
  84.         while (multiLines > maxLines)##MARK maxLines
  85.             {
  86.             j = index(multi, "\n");
  87.             out[++n] = substr(multi, 1, j);
  88.             multi = substr(multi, j + 1);
  89.             --multiLines;
  90.             ++outLine;
  91.             }
  92.         }
  93.     }
  94.  
  95. END {    if (n > 0)
  96.             {
  97.             for (i = 1; i <= n; ++i)
  98.                 print out[i] > outfile;
  99.             if (multi != "")
  100.                 print multi > outfile;
  101.             print "\n" > outfile;
  102.             close(outfile);
  103.             }
  104.         if (numDid && document != 0)
  105.             print "Total replacements: ", numDid, "\n"
  106.         }
  107.  
  108. #Dynabyte will freely distribute any nifty hAWK programs that you submit.
  109. #You get: author credit at top of the program; the satisfaction of
  110. #knowing you have made the world a better place; and in the Apple tradition,
  111. #a free kudo.